$(document).ready(function(e){
	createPageTab();
	createSellerAssessListTab();
	readyAllBrand("brandSelect", null);
	findList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var list = null;
var curPageNum = "";
function findList(pageNow){
	var tmp2=root+"/admin/seller-assess!findList";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "task.content":$("#taskInput").val(),
				"seller.name":$("#sellerInput").val(), "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			if(list!=null) createSellerAssessListTab();
			totalPageNum = json.totalPageNum;
			list = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].seller.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].seller.jobNumber+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].seller.post+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].task.content+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].yibiao+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].kaoqin+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].zhishi+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].jiqiao+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdate('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="del('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findList(json.totalPageNum);});
		}
	});
}

function findAllSeller(divId){
	var tmp2=root+"/admin/seller!findAll";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var htmlStr = "";
			for(var i=0; i<json.length; i++){
				htmlStr += '<option value="'+json[i].id+'">'+json[i].name+'</option>';
			}
			$("#"+divId).html(htmlStr);
		}
	})
}

function findAllTask(divId){
	var tmp2=root+"/admin/task!findAll";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{},
		dataType:"json",
		success: function(json){
			var htmlStr = "";
			for(var i=0; i<json.length; i++){
				htmlStr += '<option value="'+json[i].id+'">'+json[i].content+'</option>';
			}
			$("#"+divId).html(htmlStr);
		}
	})
}

function showAdd(){
	findAllSeller("sellerSelect_add");
	findAllTask("taskSelect_add");
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAdd(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function add(){
	if($("#yibiao_add").val()=="" || $("#yibiao_add").val()==null){
		alert("仪表不能为空！");
		return;
	}
	if($("#kaoqin_add").val()=="" || $("#kaoqin_add").val()==""){
		alert("考勤不能为空！");
		return;
	}
	if($("#zhishi_add").val()!=$("#zhishi_add").val()){
		alert("知识不能为空！");
		return;
	}
	if($("#jiqiao_add").val()=="" || $("#jiqiao_add").val()==""){
		alert("技巧不能为空！");
		return;
	}
	$("#addForm").submit();
}

function del(id){
	var ids = "";
	if(id==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一条数据！");
			return;
		}
	}
	
	var tmp2=root+"/admin/seller-assess!delete";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"sellerAssess.id":id, "ids":ids, "page.curPageNum":curPageNum, "isDesc": isDesc, "task.content":$("#taskInput").val(),
				"seller.name":$("#sellerInput").val(), "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findList(json.curPageNum);
		}
	});
}

function showUpdate(pageNow, index){
	findAllSeller("sellerSelect_u");
	findAllTask("taskSelect_u");
	
	$("#sellerSelect_u").val(list[index].seller.id);
	$("#taskSelect_u").val(list[index].task.id);
	
	$("#sellerAssessId_u").val(list[index].id);
	$("#yibiao_u").val(list[index].yibiao);
	$("#kaoqin_u").val(list[index].kaoqin);
	$("#zhishi_u").val(list[index].zhishi);
	$("#xiaoshou_u").val(list[index].jiqiao);
	
	$("#backGroud").show();
	$("#updateDiv").show();
	
	$("#updateButton").bind("click",function(){update(pageNow, index);});
}

function closeUpdate(){
	$("#updateForm")[0].reset();
	$("#updateButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function update(pageNow, index){
	if($("#yibiao_u").val()=="" || $("#yibiao_u").val()==null){
		alert("仪表不能为空！");
		return;
	}
	if($("#kaoqin_u").val()=="" || $("#kaoqin_u").val()==""){
		alert("考勤不能为空！");
		return;
	}
	if($("#zhishi_u").val()!=$("#zhishi_u").val()){
		alert("专业知识不能为空！");
		return;
	}
	if($("#xiaoshou_u").val()=="" || $("#xiaoshou_u").val()==""){
		alert("销售技巧不能为空！");
		return;
	}
	var tmp2=root+"/admin/seller-assess!update";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"sellerAssess.id":$("#sellerAssessId_u").val(), "sellerAssess.yibiao": $("#yibiao_u").val(), "sellerAssess.kaoqin":$("#kaoqin_u").val(),
				"sellerAssess.zhishi":$("#zhishi_u").val(), "sellerAssess.jiqiao":$("#xiaoshou_u").val(), "seller.id":$("#sellerSelect_u").val(),"task.id":$("#taskSelect_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdate();
			if(json.code=="n"){
				alert("修改失败！");
			}else{
				findList(json.curPageNum);
			}
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findList(p);
	}
}